Serve OAuth protected resource metadata (RFC 9728) on the MCP server#48
Open
ennsharma wants to merge 1 commit into
Open
Conversation
The bearer-auth middleware already advertises a resource_metadata URL in 401 WWW-Authenticate headers, but no route served it, so spec-compliant clients following the pointer got a 404 (modelcontextprotocol#22). In EXTERNAL mode there was no way to discover the authorization server at all besides the legacy RFC 8414 shim. - Serve RFC 9728 protected resource metadata at /.well-known/oauth-protected-resource (the URL advertised in 401s) and /.well-known/oauth-protected-resource/mcp (RFC 9728 path insertion), listing the mode-appropriate authorization server - Keep the legacy /.well-known/oauth-authorization-server shim for clients of the 2025-03-26 spec revision, and label it as such Fixes modelcontextprotocol#22 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #22.
The MCP module's
requireBearerAuthalready advertises aresource_metadataURL in 401WWW-Authenticateheaders (viagetOAuthProtectedResourceMetadataUrl), but nothing ever served that path — so clients that follow the spec's discovery flow got a 404. In EXTERNAL mode this made authorization-server discovery effectively impossible except through the legacy shim.Changes (in
src/index.ts, mirroring the existing inline metadata shim style):/.well-known/oauth-protected-resource(the exact URL advertised in 401s) and/.well-known/oauth-protected-resource/mcp(RFC 9728 §3.1 path insertion, for clients that derive the metadata URL from the/mcpendpoint themselves).authorization_serverspoints at the in-process server in INTERNAL mode and atAUTH_SERVER_URLin EXTERNAL mode./.well-known/oauth-authorization-servershim rather than removing it as the issue suggests: clients of the 2025-03-26 spec revision discover auth by querying that path on the MCP server directly, and in EXTERNAL mode the shim correctly points them at the external AS endpoints. It's now commented as a back-compat shim; happy to remove it instead if you'd prefer strict current-spec behavior.Verified by booting both modes and curling:
AUTH_MODE=external AUTH_SERVER_URL=http://localhost:3001): both PRM endpoints returnauthorization_servers: ["http://localhost:3001"], and the URL in the 401'sWWW-Authenticate: ... resource_metadata="…"now resolves.npm run build,npx eslint, and the jest suite (79 tests, 6 suites) all pass.🤖 Generated with Claude Code